﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SSS.Common;

namespace SSS.include
{
    public partial class CheckCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 设置验证码类型
                Common.CheckCodeHelper.CheckCodeType type = Common.CheckCodeHelper.CheckCodeType.数字;
                switch (Request.Params["type"])
                {
                    case "char":
                        type = Common.CheckCodeHelper.CheckCodeType.字母;
                        break;
                    case "chengyu":
                        type = Common.CheckCodeHelper.CheckCodeType.成语;
                        break;
                    case "mix":
                        type = Common.CheckCodeHelper.CheckCodeType.组合;
                        break;
                    default:
                        type = Common.CheckCodeHelper.CheckCodeType.数字;
                        break;
                }
                // 设置验证码大小
                Common.CheckCodeHelper.CheckCodeSize size = Common.CheckCodeHelper.CheckCodeSize.小;
                if (Request.Params["size"] == "l")
                {
                    size = Common.CheckCodeHelper.CheckCodeSize.大;
                }
                // 生成验证码
                Common.CheckCodeHelper code = new Common.CheckCodeHelper(type, size);
                // 保存用于验证的Cookie
                Common.CookieHelper.SetCookie("CheckCode", code.Text);
                Common.CookieHelper.SetCookie("CheckCodeCommentate", code.Commentate);
                // 将验证图片写回客户端
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                code.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                Response.ClearContent();
                Response.ContentType = "image/Gif";
                Response.BinaryWrite(ms.ToArray());
                Response.End();
            }
        }
    }
}